package com.zhihaoliang.moshi.biz.parser

import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.zhihaoliang.moshi.biz.models.Plant
import com.zhihaoliang.moshi.util.log
import com.zhihaoliang.moshi.util.logJson

/**
 * @author 支豪亮
 * @date 2023/1/20 11:45
 * @des 解析 List
 */

val dataList = """
[
    {
        "plantId":"malus-pumila",
        "name":"Apple",
        "description":"An apple i",
        "growZoneNumber":3,
        "wateringInterval":30,
        "imageUrl":"Apple_orchard_in_Tasmania.jpg"
    },
    {
        "plantId":"beta-vulgaris",
        "name":"Beet",
        "description":"The beetroot is ",
        "growZoneNumber":2,
        "wateringInterval":7,
        "imageUrl":"Beetroot_jm26647.jpg"
    },
    {
        "plantId":"coriandrum-sativum",
        "name":"Cilantro",
        "description":"Coriander, ",
        "growZoneNumber":2,
        "wateringInterval":2,
        "imageUrl":"A_scene_of_Coriander_leaves.JPG"
    }
]
""".trimIndent()

fun parserJsonList(){
    val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
    val plantListType = Types.newParameterizedType(List::class.java, Plant::class.java)
    val plantListAdapter = moshi.adapter<List<Plant>>(plantListType)

    val plantList = plantListAdapter.fromJson(dataList) ?: emptyList()

    log(plantList.toString())

    logJson<List<Plant>>(plantList,plantListAdapter)
}